﻿#region

using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using InfoTech.VinhSon.Bussiness;
using InfoTech.VinhSon.Models;
using Telerik.Web.Mvc;

#endregion

namespace VinhSon.Areas.Administrator.Controllers
{
    public class GroupController : BaseAdministratorController
    {
        public ActionResult Index()
        {
            return View();
        }

        [GridAction]
        public ActionResult GetList()
        {
            return View(new GridModel(UnitOfWork.GroupRepository.Select(c => c.Inactive == false || c.Inactive == null)
                                                .Select(c => new
                                                                 {
                                                                     c.Id,
                                                                     c.Name,
                                                                     c.HomePage,
                                                                     c.CategoryPage,
                                                                     c.ArticlePage,
                                                                     c.Activated,
                                                                     Layout = c.Layout.Name,
                                                                     c.Priority,
                                                                     c.FilterByDate,
                                                                     c.Index
                                                                 })));
        }

        public ActionResult Add()
        {
            ViewBag.Layouts = UnitOfWork.LayoutRepository.Select(c => c.Inactive == false || c.Inactive == null,
                                                                      c => c.Name)
                                           .Select(c => new
                                           {
                                               c.Id,
                                               c.Name
                                           })
                                           .ToList()
                                           .Select(c => new SelectListItem
                                           {
                                               Text = c.Name,
                                               Value = c.Id + ""
                                           });
            return View(new GroupModel());
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Add(GroupModel model)
        {
            if (model.FromDate.HasValue && model.ToDate.HasValue)
                if (model.FromDate.Value > model.ToDate.Value)
                    ModelState.AddModelError("ToTime", "ToDate must be greater then FromDate");
            if (ModelState.IsValid)
            {
                Group @group = model;
                if (UnitOfWork.GroupRepository.CheckInsertGroup(@group))
                {
                    group.Layout = UnitOfWork.LayoutRepository.SelectbyId(model.LayoutId ?? 0);
                    UnitOfWork.GroupRepository.Insert(@group);
                    UnitOfWork.Save();
                    return RedirectToAction("Index");
                }
            }
            ViewBag.Layouts = UnitOfWork.LayoutRepository.Select(c => c.Inactive == false || c.Inactive == null,
                                                                      c => c.Name)
                                           .Select(c => new
                                           {
                                               c.Id,
                                               c.Name
                                           })
                                           .ToList()
                                           .Select(c => new SelectListItem
                                           {
                                               Text = c.Name,
                                               Value = c.Id + ""
                                           });
            return View(model);
        }

        public ActionResult Edit(string id)
        {
            int guid;
            if (int.TryParse(id,
                            out guid))
            {
                Group webGroup = UnitOfWork.GroupRepository.SelectbyId(guid);
                if (webGroup != null)
                {
                    GroupModel webGroupModel = webGroup;

                    ViewBag.Layouts = UnitOfWork.LayoutRepository.Select(c => c.Inactive == false || c.Inactive == null,
                                                                      c => c.Name)
                                           .Select(c => new
                                           {
                                               c.Id,
                                               c.Name
                                           })
                                           .ToList()
                                           .Select(c => new SelectListItem
                                           {
                                               Text = c.Name,
                                               Value = c.Id + ""
                                           });

                    return View(webGroupModel);
                }
            }
            return RedirectToAction("Index");
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(GroupModel model)
        {
            if (model.FromDate.HasValue && model.ToDate.HasValue)
                if (model.FromDate.Value > model.ToDate.Value)
                    ModelState.AddModelError("ToTime", "ToDate must be greater then FromDate");
            if (ModelState.IsValid)
            {
                Group @group = UnitOfWork.GroupRepository.SelectbyId(model.Id);
                model.Update(@group);
                if (UnitOfWork.GroupRepository.CheckUpdateGroup(@group))
                {
                    @group.Layout = UnitOfWork.LayoutRepository.SelectbyId(model.LayoutId ?? 0);
                    UnitOfWork.GroupRepository.Update(@group);
                    UnitOfWork.Save();
                    return RedirectToAction("Index");
                }
            }
            ViewBag.Layouts = UnitOfWork.LayoutRepository.Select(c => c.Inactive == false || c.Inactive == null,
                                                                      c => c.Name)
                                           .Select(c => new
                                           {
                                               c.Id,
                                               c.Name
                                           })
                                           .ToList()
                                           .Select(c => new SelectListItem
                                           {
                                               Text = c.Name,
                                               Value = c.Id + ""
                                           });
            return View(model);
        }

        [HttpPost]
        public ActionResult Delete(string id)
        {
            if (id != null)
            {
                string[] s = id.Split(',');
                int guid;
                List<int> list = (from s1 in s
                                  where int.TryParse(s1,
                                                     out guid)
                                  select int.Parse(s1)).ToList();

                if (UnitOfWork.GroupRepository.CheckDeleteGroup(list))
                {
                    UnitOfWork.GroupRepository.Delete(list);
                    UnitOfWork.Save();
                }
            }
            return Json(new
                            {
                                status = 1,
                                msg = "Update Successfully"
                            },
                        JsonRequestBehavior.DenyGet);
        }
    }
}
